package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.mapper.*;
import com.atguigu.gmall.product.service.ManageService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.ConnectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;

@Service
public class ManageServiceImpl implements ManageService {


    @Autowired
    private BaseCategory1Mapper baseCategory1Mapper;

    @Autowired
    private BaseCategory2Mapper baseCategory2Mapper;

    @Autowired
    private BaseCategory3Mapper baseCategory3Mapper;

    @Autowired
    private BaseAttrInfoMapper baseAttrInfoMapper;

    @Autowired
    private BaseAttrValueMapper baseAttrValueMapper;

    @Autowired
    private BaseTrademarkMapper baseTrademarkMapper;

    //查询一级分类集合实现方法
    @Override
    public List<BaseCategory1> getCategory1() {

        return baseCategory1Mapper.selectList(null);
    }

    //根据一级分类id查询二级分类实现方法
    @Override
    public List<BaseCategory2> getCategory2(Long category1Id) {

        QueryWrapper wrapper = new QueryWrapper<BaseCategory2>();
        wrapper.eq("category1_id", category1Id);
        List<BaseCategory2> baseCategory2List = baseCategory2Mapper.selectList(wrapper);
        return baseCategory2List;
    }

    //根据二级分类id查询三级分类实现方法
    @Override
    public List<BaseCategory3> getCategory3(Long category2Id) {

        QueryWrapper wrapper = new QueryWrapper<BaseCategory2>();
        wrapper.eq("category2_id", category2Id);
        List<BaseCategory3> baseCategory3List = baseCategory3Mapper.selectList(wrapper);
        return baseCategory3List;
    }

    //根据id获取平台属性实现方法  编写xml文件
    @Override
    public List<BaseAttrInfo> attrInfoList(Long category1Id, Long category2Id, Long category3Id) {
        //调用mapper
        return baseAttrInfoMapper.attrInfoList(category1Id,category2Id,category3Id);
    }

    //添加或修改平台属性实现方法
    @Override
    public void saveAttrInfo(BaseAttrInfo baseAttrInfo) {
        //平台属性包括平台属性列表base_attr_info和平台属性值列表base_attr_value
        //第一步：添加或修改平台属性列表  base_attr_info
        //判断是添加还是修改
        if (baseAttrInfo.getId() != null) {
            //修改,根据id修改,先根据id删除，再添加
            baseAttrInfoMapper.updateById(baseAttrInfo);
        } else {
            //添加
            baseAttrInfoMapper.insert(baseAttrInfo);
        }


        //第二步：添加或修改平台属性值列表  base_attr_value
        //可以通过先删除属性值再添加属性值的方法进行修改
        //删除条件：为属性值列表的attr_id=baseAttrInfo.getId()
        QueryWrapper<BaseAttrValue> wrapper = new QueryWrapper<>();
        wrapper.eq("attr_id", baseAttrInfo.getId());
        baseAttrValueMapper.delete(wrapper);

        //添加属性值列表的值,先获取页面传过来的所有平台属性值数据
        List<BaseAttrValue> attrValueList = baseAttrInfo.getAttrValueList();
        if (!CollectionUtils.isEmpty(attrValueList)) {
            for (BaseAttrValue baseAttrValue : attrValueList) {
                //传入attr_id，为外键
                baseAttrValue.setAttrId(baseAttrInfo.getId());
                baseAttrValueMapper.insert(baseAttrValue);
            }

        }


    }

    //通过平台属性id获取平台属性对象,再将最新属性值作更新
    @Override
    public BaseAttrInfo getAttrInfo(Long attrId) {
        BaseAttrInfo baseAttrInfo = baseAttrInfoMapper.selectById(attrId);
        //查询到最新平台属性值的集合数据放到平台属性中
        baseAttrInfo.setAttrValueList(this.getAttrValueList(attrId));
        return baseAttrInfo;
    }

    //根据平台属性id  attrId   获取平台属性值list实现方法
    private List<BaseAttrValue> getAttrValueList(Long attrId) {

        QueryWrapper<BaseAttrValue> wrapper = new QueryWrapper<>();
        wrapper.eq("attr_id", attrId);
        List<BaseAttrValue> baseAttrValueList = baseAttrValueMapper.selectList(wrapper);
        return baseAttrValueList;
    }

    //获取品牌列表带分页实现方法
    @Override
    public IPage baseTrademark(Integer page, Integer limit) {
        IPage<BaseTrademark> baseTrademarkIPage = baseTrademarkMapper.selectPage(new Page<BaseTrademark>(page, limit), null);
        return baseTrademarkIPage;
    }
}
